limsEquipScrap.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-equip-scrap-edit-list-grid',
            url: limsEquipScrap.baseUrl + '/queries/raw',
            service: 'limsEquipScrapServiceImpl',
            dbTable: 'T_LIMS_EQUIP_SCRAP',
            columns: limsEquipScrap.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'equipCode' && _this.loadDetailPage(row.id)
            },
            deleteFormatter: function (row) {
                return row.equipCode || row.id
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.createLimsModal({
                        modalTitle: 'BASEMODULE.RM.EQUIPMENT.EQUIPSCRAPS.MODAL.ADD',
                        width: 400,
                        height: 250,
                        onConfirm: function () {
                            var form = Gikam.getComp('lims-equip-scrap-form');
                            if (form.validate()) {
                                var data = form.getData()
                                Gikam.postText(limsEquipScrap.baseUrl, Gikam.getJsonWrapper(null, [null, [data]])).done(function () {
                                    Gikam.getComp('lims-equip-scrap-edit-list-grid').refresh()
                                })
                                Gikam.getLastModal().close(data)
                            }
                        },
                        m: {
                            type: 'form',
                            id: 'lims-equip-scrap-form',
                            disableNotEmptyValidate: true,
                            fields: limsEquipScrap.getBaseInfoFormFields(),
                            columns: 1
                        },
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-equip-scrap-edit-list-grid').deleteRows(limsEquipScrap.baseUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.submit()
                }
            }]
        }
    },

    submit: function () {
        var grid = Gikam.getComp('lims-equip-scrap-edit-list-grid')
        Gikam.create('workflow').submit({
            data: grid.getSelections(),
            pageObject: limsEquipScrap,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh()
        })
    },

    loadDetailPage: function (id) {
        workspace.window.load(limsEquipScrap.baseUrl + '/page/detail' + Gikam.param({
            id: id,
            step: 'edit'
        }))
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-equip-scrap-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
